/*
 * Copyright (c) 2010, Nikolaus Moll
 * All rights reserved.
 */
package net.miblounge.gradecalculator.core.olddescriptors;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlType;

import net.miblounge.gradecalculator.core.interfaces.IModuleIdentificator;
import net.miblounge.gradecalculator.core.interfaces.IModuleListItem;

@XmlRootElement(namespace = "http://notenrechner.miblounge.net/")
@XmlType( 
 propOrder = { "name", "moduleType", "moduleListECTS", "pickCount", "sortOrder", "moduleLists" } 
) 
public class SelectableModuleLists implements IModuleListItem, IModuleIdentificator {
	private List<ModuleList> moduleLists;
	private String name;
	private int pickCount;
	private float moduleListECTS;
	private int moduleHoursPerWeek;
	
	private int sortOrder;
	
	private IModuleIdentificator parent;

	public SelectableModuleLists()
	{
		this("", 0);
	}
	
	public SelectableModuleLists(final String name)
	{
		this(name, 0);
	}

	public SelectableModuleLists(final String name, final int pickCount)
	{
		this.name = name;
		this.pickCount = pickCount;
		moduleLists = new ArrayList<ModuleList>();
	}

	@XmlTransient
	public IModuleIdentificator getParent()
	{
		return parent;
	}
	public void setParent(final IModuleIdentificator parent)
	{
		this.parent = parent;
	}
	
	
	@XmlElement(name = "name" )
	public String getName() {
		return name;
	}
	public void setName(final String name) {
		this.name = name;
	}

	@XmlElement(name = "pickcount" )
	public int getPickCount() {
		return pickCount;
	}
	public void setPickCount(final int pickCount) {
		this.pickCount = pickCount;
	}

	@XmlElement(name = "ects" )
	public float getModuleListECTS() {
		return moduleListECTS;
	}
	public void setModuleListECTS(final float ects) {
		this.moduleListECTS = ects;
	}

	@Override
	public int getSortOrder() {
		return sortOrder;
	}
	public void setSortOrder(final int sortOrder) {
		this.sortOrder = sortOrder;
	}
	
	@XmlElementWrapper(name = "modulelists")
	@XmlElement(name = "modulelist" )
	public List<ModuleList> getModuleLists()
	{
		return moduleLists;
	}

	@Override
	public float getECTS() {
		update();
		return pickCount * moduleListECTS;
	}

	@Override
	@XmlTransient
	public int getHoursPerWeek()
	{
		update();
		return pickCount * moduleHoursPerWeek;
	}
	
	@Override
	@XmlElement(name = "type" )
	public ModuleType getModuleType() {
		return ModuleType.SelectableModuleLists;
	}
	
	@Override
	public String getIdentifier() {
		if (parent != null) {
			return parent.getIdentifier() + "." + name;
		}
		return name;
	}

	public void addModuleList(final ModuleList moduleList)
	{
		moduleLists.add(moduleList);
		moduleList.setParent(this);
	}

	public void removeModuleList(final ModuleList moduleList)
	{
		moduleLists.remove(moduleList);
	}

	@Override
	public int compareTo(final IModuleListItem o) {
		return sortOrder - o.getSortOrder();
	}

	@Override
	public String toString()
	{
		return getName();
	}
	
	protected void update()
	{
		float lastModuleECTS = -1;
		
		for (ModuleList list: moduleLists) {
			if (lastModuleECTS < 0) {
				lastModuleECTS = list.getECTS();
			} else {
				if (lastModuleECTS != list.getECTS()) {
					lastModuleECTS = 0;
					break;
				}
			}
		}

		int lastModuleHoursPerWeek = -1;
		for (ModuleList list: moduleLists) {
			if (lastModuleHoursPerWeek < 0) {
				lastModuleHoursPerWeek = list.getHoursPerWeek();
			} else {
				if (lastModuleHoursPerWeek != list.getHoursPerWeek()) {
					lastModuleHoursPerWeek = 0;
					break;
				}
			}
		}
		
		if (lastModuleECTS > 0) {
			moduleListECTS = lastModuleECTS;
		}
		if (lastModuleHoursPerWeek > 0) {
			moduleHoursPerWeek = lastModuleHoursPerWeek;
		}
	}

	public void insertModuleList(final ModuleList moduleList, final ModuleList moduleListBefore) {
		final int index = moduleLists.indexOf(moduleListBefore);
		moduleLists.add(index, moduleList);
		moduleList.setParent(this);
	}
	
	// TODO
	// implement hashcode
}
